//
//  ANMineTickerTopView.m
//  WeiYiMei
//
//  Created by 李叶夫 on 2019/9/22.
//  Copyright © 2019 liyefu. All rights reserved.
//

#import "ANMineTickerTopView.h"

@interface ANMineTickerTopView ()

@property (nonatomic, strong) UIButton *allBtn;
@property (nonatomic, strong) UIButton *useBtn;
@property (nonatomic, strong) UIView *sliderView;

@property (nonatomic) NSInteger currentSelectIndex;

@end

@implementation ANMineTickerTopView

- (void)setupSubViews
{
    self.allBtn = [[UIButton alloc] init];
    [self.allBtn setTitle:@"全部券" forState:UIControlStateNormal];
    [self.allBtn setTitleColor:kColor777777 forState:UIControlStateNormal];
    [self.allBtn setTitleColor:ThemeColor forState:UIControlStateSelected];
    self.allBtn.titleLabel.font = [UIFont systemFontOfSize:15 weight:UIFontWeightMedium];
    [self.allBtn addTarget:self action:@selector(actionTap:) forControlEvents:UIControlEventTouchUpInside];
    self.allBtn.selected = YES;
    self.currentSelectIndex = 0;
    
    self.useBtn = [[UIButton alloc] init];
    [self.useBtn setTitle:@"已使用" forState:UIControlStateNormal];
    [self.useBtn setTitleColor:kColor777777 forState:UIControlStateNormal];
    [self.useBtn setTitleColor:ThemeColor forState:UIControlStateSelected];
    self.useBtn.titleLabel.font = [UIFont systemFontOfSize:15 weight:UIFontWeightMedium];
    [self.useBtn addTarget:self action:@selector(actionTap:) forControlEvents:UIControlEventTouchUpInside];
    
    self.sliderView = [[UIView alloc] init];
    self.sliderView.backgroundColor = ThemeColor;
}

- (void)setupConstraints
{
    [self addSubview:self.allBtn];
    [self addSubview:self.useBtn];
    [self addSubview:self.sliderView];
    
    [self.allBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(20);
        make.top.equalTo(self);
        make.height.equalTo(@50);
    }];
    [self.useBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.allBtn.mas_right).offset(56);
        make.top.equalTo(self);
        make.height.equalTo(@50);
    }];
    
    [self.sliderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.allBtn);
        make.bottom.equalTo(self.allBtn.mas_bottom);
        make.width.equalTo(@20);
        make.height.equalTo(@2);
    }];
}

- (void)actionTap:(UIButton *)sender
{
    NSInteger index = 0;
    if (sender == self.allBtn) {
        index = 0;
    }
    if (sender == self.useBtn) {
        index = 1;
    }
    if ([self.delegate respondsToSelector:@selector(ANMineTickerTopView:didSelectedIndex:lastSelectedIndex:)]) {
        [self.delegate ANMineTickerTopView:self didSelectedIndex:index lastSelectedIndex:self.currentSelectIndex];
    }
    [self setSelectedIndex:index];
}

- (void)setSelectedIndex:(NSInteger)index
{
    if (index == 0) {
        self.allBtn.selected = true;
        self.useBtn.selected = false;
    }
    if (index == 1) {
        self.allBtn.selected = false;
        self.useBtn.selected = true;
    }
    [self animationSlider:index];
    self.currentSelectIndex = index;
}

- (void)animationSlider:(NSInteger)index
{
    if (index == 0) {
        [self.sliderView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.allBtn);
            make.bottom.equalTo(self.allBtn.mas_bottom);
            make.width.equalTo(@20);
            make.height.equalTo(@2);
        }];
    }
    if (index == 1) {
        [self.sliderView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.useBtn);
            make.bottom.equalTo(self.allBtn.mas_bottom);
            make.width.equalTo(@20);
            make.height.equalTo(@2);
        }];
    }
    [UIView animateWithDuration:0.4 animations:^{
        [self layoutIfNeeded];
    }];
}

- (void)setCouponNum:(int)couponNum
{
    _couponNum = couponNum;
    [self.allBtn setTitle:[NSString stringWithFormat:@"全部券(%d张)",couponNum] forState:UIControlStateNormal];
}

- (void)setUseNum:(int)useNum
{
    _useNum = useNum;
    [self.useBtn setTitle:[NSString stringWithFormat:@"已使用(%d张)",useNum] forState:UIControlStateNormal];
}

@end
